<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <input @change="onChange" type="file" hidden ref="inputFile">
    <van-cell @click="$refs.inputFile.click()" title="头像" is-link>
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isShowUserEdit = true"
      :value="user.name"
      is-link
    />
    <van-cell
      @click="isShowUserEditSex = true"
      title="性别"
      :value="user.gender ? '女' : '男'"
      is-link
    />
    <van-cell @click="isShowUserEditBirthday=true" title="生日" :value="user.birthday" is-link />
    <!-- 编辑昵称弹层 -->
    <van-popup
      v-model="isShowUserEdit"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <user-edit
        v-if="isShowUserEdit"
        v-model="user.name"
        @close="isShowUserEdit = false"
      />
    </van-popup>
    <!-- 编辑性别弹层 -->
    <van-popup v-model="isShowUserEditSex" position="bottom">
      <user-edit-sex
        v-if="isShowUserEditSex"
        v-model="user.gender"
        @close="isShowUserEditSex = false"
      />
    </van-popup>
    <!-- 编辑生日弹层 -->
    <van-popup v-model="isShowUserEditBirthday" position="bottom">
      <user-edit-birthday v-model="user.birthday" v-if="isShowUserEditBirthday" @close="isShowUserEditBirthday=false" />
    </van-popup>
    <!-- 编辑头像弹层 -->
    <van-popup v-model="isShowUserEditAvatar" position="bottom" style="height:100%">
      <user-edit-avatar @update-photo="user.photo = $event" v-if="isShowUserEditAvatar" @close="isShowUserEditAvatar=false" :img="imgUrl" />
    </van-popup>
  </div>
</template>

<script>
import { getUser } from '@/api/user.js'
import UserEdit from './components/UserEdit.vue'
import UserEditSex from './components/UserEditSex.vue'
import UserEditBirthday from './components/UserEditBirthday.vue'
import UserEditAvatar from './components/UserEditAvatar.vue'

export default {
  components: { UserEdit, UserEditSex, UserEditBirthday, UserEditAvatar },
  data () {
    return {
      user: {},
      isShowUserEdit: false,
      isShowUserEditSex: false,
      isShowUserEditBirthday: false,
      isShowUserEditAvatar: false,
      imgUrl: ''
    }
  },

  created () {
    this.loadUserInfo()
  },

  methods: {
    async loadUserInfo () {
      try {
        const res = await getUser()
        console.log(res)
        this.user = res
      } catch (err) {
        console.log('cuo')
      }
    },
    onChange (e) {
      if (e.target.value === '') return
      this.isShowUserEditAvatar = true
      console.log(e.target.files[0])
      this.imgUrl = URL.createObjectURL(e.target.files[0])
      e.target.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>
